<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$return['basic']=$this->plugin_setting('basic');
if($_RQ['op']=='main'){
    //接受

    
    $return['post'] = array('klcode'=>'');
    exi($return);
    
}
if($_RQ['op']=='post'){
    $postdata=get_postdata();
    if(!$postdata['klcode']){
	    exi('请输入激活码','error');
	}
	//查询该激活码的推广主
	$member = pdo_get('jy_aixiaoyi_member',array('uniacid'=>$_SESSION['uniacid'],'klcode'=>trim($postdata['klcode'])));
	//此处为激活的逻辑 绑定关系 agent_id
	$member_update = array('agent_id'=>$member['mid']);
	//自己不能绑定自己
	if($member['mid']==$_SESSION['uid']){
	    exi('激活失败自己不能激活自己','error');
	}
	if(!$member['mid']){
	    exi('该激活码无效','error');
	}
	//如果已经填加过去
	$own_user = pdo_get('jy_aixiaoyi_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	
	if($own_user['agent_id']){
	    exi('已经激活不可重复领取','error');
	}
	$this->update('member',$member_update,array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	model('core/member')->member_credit($_SESSION['uid'],'credit3',$return['basic']['bi_koulin'],true,'jy_aixiaoyi','用户激活码激活获取金币','bi_jihuom'.$_SESSION['uid']);
	$newown_user = pdo_get('jy_aixiaoyi_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	if($newown_user['agent_id']){
		//$agent=$this->get('member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$member['mid']));
		//邀请奖励(推广人头激活费)
		$tgr = pdo_get('jy_aixiaoyi_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$newown_user['agent_id']));
		$level = pdo_get('jy_aixiaoyi_level',array('uniacid'=>$_SESSION['uniacid'],'id'=>$tgr['level_id']));
		$order = array(
           'paytype'=>'credit',
           'ordersn'=>'TJ'.date('YmdHis').rand(0,999),
           'mid'=>$_SESSION['uid'],
           'pay_id'=>0,
           'price'=>$level['fee_per1'],
           'plugin_name'=>'jy_aixiaoyi'
        );
		//$this->model('commission')->create_commission($agent,17,2);
		$this->model('commission')->create($order,2);
	}
	exi('激活成功,快来使用吧','','jy_aixiaoyi/jiaoben/');
}
?>